import fs = require("fs")
import path = require("path")
import { HallServer_SocketMsgHandler } from "../../../../hall_server/HallServer_SocketMsgHandler";
import { ClubModel } from "../../../../hall_server/model/ClubModel";
import { ErrorCode } from "../../../config/ErrorCode";
import { ServerConfig } from "../../../config/ServerConfig";
import { HttpUtil } from "../../utils/HttpUtil";
import { Logger } from "../../utils/Logger";
import { SocketConnection } from "./SocketConnection";

export class SocketConnectionManager {

    public static instance: SocketConnectionManager = new SocketConnectionManager();

    public socketConnMap: Map<number, SocketConnection> = new Map<number, SocketConnection>();

    /* 
   增加客户端socket连接
    */
    public addSocketConnection(conn: SocketConnection) {
        this.socketConnMap.set(conn.id, conn);
    }

    public getSocketConnection(id: number) {
        return this.socketConnMap.get(id);
    }

    public getSocketConnectionByUser(account: string): Array<SocketConnection> {
        let result: Array<SocketConnection> = []
        this.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (!conn.isClosing && conn.account && conn.account == account) {
                result.push(conn)
                return;
            }
        });
        return result
    }

    public killAllByWeihu() {
        this.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (!conn.isClosing && conn.account) {
                conn.sendErrorToClient(0, ErrorCode.Code.Server_Weihu, ErrorCode.code2Msg.get(ErrorCode.Code.Server_Weihu))
                conn.gotoClose();
            }
        });
    }

    public clearAll() {
        // this.socketConnMap.forEach((conn: SocketConnection, key: number) => {
        //     if (!conn.isClosing && conn.account) {
        //         this.sendErrorToClient(socketConnect, ErrorCode.Code.Server_Weihu, ErrorCode.code2Msg.get(ErrorCode.Code.Server_Weihu))
        //         HallServer_SocketMsgHandler.sendErrorToClient(conn, err)
        //     }
        // });
    }

    /* 
    删除客户端socket连接
     */
    public removeSocketConnection(removeOne: SocketConnection) {
        if (!removeOne) {
            return;
        }
        this.socketConnMap.delete(removeOne.id);
    }

    public getConnectionCount() {
        let count: number = 0
        this.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (conn.isLogin()) {
                if (conn.user) {
                    count++
                }
                // Logger.log("getConnectionCount==", conn.account, conn.user)
            }
        });
        return count;
    }

    public getOnlineUserList(){
        let useridList:Array<number> = [

        ]
        SocketConnectionManager.instance.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (conn.isLogin()) {
                if (conn.user) {
                    useridList.push(conn.user.userid)
                }
            }
        });
        return useridList
    }

    public getClubOnlineCount(clubid: number) {
        let count: number = 0;
        SocketConnectionManager.instance.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (conn.isLogin()) {
                if (conn.user) {
                  
                }
            }
        });
        return count
    }

    public isUserOnline(userid: number) {
        let isOnline: boolean = false
        SocketConnectionManager.instance.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (conn.isLogin()) {
                if (conn.user && conn.user.userid == userid) {
                    isOnline = true
                    return;
                }
            }
        });
        return isOnline
    }

    public getSocketConnectionByUserid(userid: number): SocketConnection {
        let connection: SocketConnection = null
        this.socketConnMap.forEach((conn: SocketConnection, key: number) => {
            if (conn.isLogin()) {
                if (conn.user && conn.user.userid == userid) {
                    connection = conn
                    return conn;
                }
            }
        });
        return connection
    }

    public notify_club_open_or_close(clubid: number, state: number) {
        // this.socketConnMap.forEach((conn: SocketConnection, key: number) => {
        //     if (conn.isLogin()) {
        //         if (conn.user) {
        //             let club: ClubModel =  conn.user.getClub(clubid)
        //             if (club) {
        //                 club.state = state;
        //             }
        //         }
        //     }
        // });
    }


}